



project , uses("candidate_elections.dta")

	use "candidate_elections.dta", clear //open data
	replace party = "Other" if inlist(party, "Labour", "Liberal (Dem.)", "Conservative")==0
	drop if by_election==1
	drop if spending_filed==0
	drop if year==2005
	
	preserve
		gen pct = .
		gen category = ""
		foreach p in lib lab con other {
			gen est_`p' = .
			}
		local i = 1
		foreach v in ads managers meetings misc personal facilities staff {
			replace pct = 100*spending_`v' / spending_total
			reg pct  lib lab con other, noconst 
			replace category = proper("`v'") if _n==`i'
			foreach p in lib lab con other {
				replace  est_`p' = _b[`p'] if _n==`i'
				}
			local i = `i'+1
		}
		replace category = "Misc." if category=="Misc"
		replace category = "Agents" if category=="Managers"
		collapse (mean) est_* , by(category)
		keep if est_con!=.
		sort est_con
		gen rank = _n
		labmask rank, values(category)
		
			twoway 	 (dot est_con rank ,horizontal  ndots(1) msymbol(diamond) mfcolor(white) mcolor(blue) msize(medium) ) ///
					 (dot est_lab rank ,horizontal  ndots(1) msymbol(0) mfcolor(white) mcolor(red) msize(medium)) ///
					 (dot est_lib rank ,horizontal  ndots(1) msymbol(s) mfcolor(white) mcolor(gold) msize(medium) ) ///
					 (dot est_other rank ,horizontal  ndots(1) msymbol(triangle) mfcolor(white) mcolor(gs12) msize(medium)) ///
					 , ytitle("") xtitle("% of Total Expenditure") xlabel(0 (10) 80, labsize(medium)) ylabel(1 (1) 7,valuelabel angle(horizontal) labsize(medium) ) ///
					 legend( order(1 "Conservative" 2 "Labour" 3 "Liberal (Dem.)" 4 "Other" ) position(5) ring(0) ) ///
					 saving(figures/type_by_party.gph, replace)
	restore
				 
	
	
	*Shares
	collapse (sum) spending_allemployees	spending_ads	spending_meetings	spending_facilities	spending_misc spending_personal spending_total, by(year)
	
	
	gen meetings = 100*(spending_meetings) / spending_total
	labe var meetings "Meetings, %"	
	gen ads = 100* (spending_meetings + spending_ads) / spending_total
	label var ads "Ads, %"
	*gen managers = 100*(spending_managers + spending_ads) / spending_total
	*label var managers "Managers, %"
	gen employees = 100*(spending_allemployees + spending_meetings+ spending_ads) / spending_total
	label var employees "Employees, %"
	gen facilities = 100*(spending_facilities +spending_allemployees + spending_meetings+ spending_ads) / spending_total
	label var facilities "Facilities, %"
	gen personal = 100*(spending_personal +spending_meetings + spending_facilities +spending_allemployees + spending_ads) / spending_total
	labe var personal "Personal, %"
	gen misc = 100
	label var misc "Misc., %"
	twoway  (area  misc personal  facilities employees ads  meetings year )  , ytitle("Expenditure Composition" ,  size(medium)) tlabel(1885 (10) 2015, labsize(medium) angle(45) ) ///
	text(2.5 1930 "% Public Meetings", color(white)) text( 30 1930 "% Advertisement" , color(white)) text(71 1930 "% Employees", color(white)) ///
	text(83 1930 "% Facilities", color(white))	text(89 1930 "% Personal", color(white)) text(96 1930 "% Miscellaneous", color(white) ) ///
	ylabel(0 (10) 100, labsize(medium) ) legend(off)   ttitle(" ")  saving(figures/type_composition.gph, replace)
	
	
	gr combine  ./figures/type_composition.gph ./figures/type_by_party.gph , xsize(20) ysize(10) iscale(1.1)  imargin(zero)
	graph export ./figures/type_composition_and_type_by_party.pdf, replace
	project , creates("figures/type_composition_and_type_by_party.pdf")		
	
	
	
	
	*spending vs limit
	use "candidate_elections.dta", clear //open data
	**drop observations: by-elections 
	drop if by_election==1
	drop if spending_filed==0
	replace spending_limit_predicted_adj = 10*spending_limit_predicted_adj	
	replace spending_limited_adj = spending_limited_adj/1000
	keep if spending_limit_predicted_adj<250 & spending_limited_adj<250
	
	
	graph twoway ( scatter spending_limited_adj spending_limit_predicted_adj ,  msymbol(p )   mcolor(black ) )  ///
	(lfit spending_limited_adj spending_limit_predicted_adj if party=="Conservative",  lcolor(blue) lpattern(dash) lwidth(medthick) )  ///
	(lfit spending_limited_adj spending_limit_predicted_adj if party=="Liberal (Dem.)" ,  lcolor(gold) lpattern(longdash) lwidth(medthick) ) ///
	(lfit spending_limited_adj spending_limit_predicted_adj if party=="Labour",  lcolor(red) lpattern(solid) lwidth(medthick) )  ///
	(lfit spending_limited_adj spending_limit_predicted_adj if party!="Conservative" & party!="Liberal (Dem.)" & party!="Labour"   ,  lcolor(gs12) lpattern(shortdash) lwidth(medthick) ) ///
	, ytitle("Spending, £1000" , size(medium)) xtitle("Spending Limit, £1000", size(medium)) ylabel(0 (25) 250, labsize(medium) ) xlabel(0 (25) 250, labsize(medium) angle(45)) ///
		text(165 180 "Conservative", color(blue) ) text(100 210 "Liberal (Dem.)", color(gold)) text( 65 140 "Other", color(gs12)) text( 45 235 "Labour", color(red)) scale(1.25) ///
	legend(off) xsize(20) ysize(20)  saving(figures/limit_vs_spending.gph, replace)	
	
	
	use "candidate_elections", clear //open data
	replace party= "Other" if inlist(party, "Labour",  "Liberal (Dem.)", "Conservative")==0
	replace party = "Lib" if party=="Liberal (Dem.)"
	drop if by_election==1
	drop if spending_filed==0
	collapse (sum) spending_total_adj , by(party year)
	reshape wide spending_total_adj, i(year) j(party) string
	replace spending_total_adjLabour = 0 if spending_total_adjLabour==.

	
	gen lab_pct = 100*(spending_total_adjLabour) / (spending_total_adjOther + spending_total_adjLib+ spending_total_adjLabour + spending_total_adjConservative)
	gen other_pct = 100*(spending_total_adjOther+spending_total_adjLab) / (spending_total_adjOther + spending_total_adjLib+ spending_total_adjLabour + spending_total_adjConservative)
	gen lib_pct = 100*(spending_total_adjOther + spending_total_adjLab  + spending_total_adjLib) / (spending_total_adjOther + spending_total_adjLib+ spending_total_adjLabour + spending_total_adjConservative)
	gen con_pct = 100
	twoway  (area  con_pct   year, color(blue) ) || (area  lib_pct   year, color(gold) ) || (area  other_pct   year, color(gs12) ) || (area  lab_pct   year, color(red) ), ///
	ytitle("Expenditure Composition by Party" ,  size(medium)) tlabel(1885 (10) 2015 , labsize(medium) angle(45) ) ///
	text(15 1940 "Labour", color(white)) text( 36 1940 "Other" , color(white)) text(46 1938 "Liberal (Dem.)", color(white))  text(75 1940 "Conservative", color(white))  ///
	ylabel(0 (10) 100, labsize(medium) ) legend(off)   ttitle(" ")  saving(figures/partisan_composition.gph, replace) scale(1.25)
	
	gr combine ./figures/limit_vs_spending.gph ./figures/partisan_composition.gph , xsize(20) ysize(10) scale(1.2) imargin(zero)
	
	graph export ./figures/limit_vs_spending_and_partisan_composition.eps, replace // either 
	*graph export ./figures/limit_vs_spending_and_partisan_composition.pdf, replace

	
	project , creates("figures/limit_vs_spending_and_partisan_composition.eps")	
	
	
